package com.bjut.dao.impl;

import java.util.List;

import com.bjut.dao.PositionDao;
import com.bjut.domain.Position;

public class PositionDaoImpl extends HRglxtHibernateDaoSupport implements PositionDao{
	public void delete(Position position) {
		getHibernateTemplate().delete(position);
	}

	public void delete(int id) {
		getHibernateTemplate().delete(get(id));
	}

	public Position findPositionByName(String name) {
		List result=getHibernateTemplate().find("from Position as p where p.name = ?" , 
				new String[]{name});
		if (result != null && result.size() > 0)
			return (Position) result.get(0);
		else 
			return null;
	}

	public Position get(int id) {
		return (Position) getHibernateTemplate().get(Position.class, id);
	}

	public void save(Position position) {
		getHibernateTemplate().save(position);
	}

	public void update(Position position) {
		getHibernateTemplate().update(position);
	}

	public List<Position> findAll() {
		return getHibernateTemplate().find("from Position as p order by p.id");
	}
	
	public List<Position> findAllByPage(int pageNo, int pageSize) {
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		return findByPage("from Position as p order by p.id", offset , pageSize);
	}

	public long getCount() {
		List positionList = getHibernateTemplate().find("select count(p.id) from Position as p");
		if (positionList == null)
		{
			return 0;
		}
		return (Long)positionList.get(0);
	}

	public List<Position> findByDepartmentAndPage(int department_id,
			int pageNo, int pageSize) {
		if (pageNo < 1)
		{
			return null;
		}
		int offset = (pageNo - 1) * pageSize;
		return findByPage("from Position as p where department_id=? order by p.id",department_id, offset , pageSize);
	}

	public long getCountByDepartment(int department_id) {
		List positionList = getHibernateTemplate().find("select count(p.id) from Position as p where department_id=?",department_id);
		if (positionList == null)
		{
			return 0;
		}
		return (Long)positionList.get(0);
	}

}
